//
//  SXHomeViewController.swift
//  sina
//
//  Created by shengxinwei on 16/5/19.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit
import SVProgressHUD

private let statusCell: String = "statusCell"

class SXHomeViewController: SXBaseTableViewController {

    
    lazy var listViewModel: SXStatusListViewModel = SXStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if SXUserAccountViewModel.sharedAccount.userLogin == false {
            visitorView.updateUI(UIImage(named: "visitordiscover_feed_image_house")!, labString: "关注一些人，回这里看看有什么惊喜", firstBool: true)
            return
        }
        tableView.rowHeight = UITableViewAutomaticDimension
        setBar()
        
        //注册cell
        tableView.registerClass(SXStatusCell.self, forCellReuseIdentifier: statusCell)
        loadData()
        
        tableView.estimatedRowHeight = 300
        tableView.addSubview(refreshView)
        refreshView.addTarget(self, action: #selector(SXHomeViewController.loadData), forControlEvents: .ValueChanged)
        
        //实例化刷新控件
//        refreshControl = UIRefreshControl()
//         refreshControl?.addTarget(self, action: #selector(SXHomeViewController.loadData), forControlEvents: .ValueChanged)
        
        tableView.tableFooterView = pullupView
    }



    private func setBar() {
        
        let pushBtn = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(SXHomeViewController.rightBtnDidClick))
        
        navigationItem.rightBarButtonItem = pushBtn
    
    }
    
    @objc private func rightBtnDidClick() {
        //跳转界面
        let detailVc = SXDetailController()
        
        //隐藏底部tabBar.
        detailVc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(detailVc, animated: true)
    }
    
    //创建加载菊花控件
    private lazy  var pullupView: UIActivityIndicatorView = {
       let indircator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        
        return indircator
    }()
    
    var refreshView: SXRefreshControl = SXRefreshControl()


}

extension SXHomeViewController {
    
    //加载数据
    @objc private func loadData() {
        
        listViewModel.loadData(pullupView.isAnimating()) { (isSuccess) in
            //停止动画
            self.pullupView.stopAnimating()
            //self.refreshControl?.endRefreshing()
            self.refreshView.endRefreshin()
            if isSuccess == false {
                SVProgressHUD.showErrorWithStatus("网络睡觉中")
                return
            }
            self.tableView.reloadData()
        }
    }
}

// MARK: - UITableViewDataSource
extension SXHomeViewController {
  
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusViewModel.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(statusCell, forIndexPath: indexPath) as! SXStatusCell
        if indexPath.row == listViewModel.statusViewModel.count-1 {
            pullupView.isAnimating()
            print("开始执行静默加载")
            pullupView.startAnimating()
            
            //加载数据
            loadData()
            
        }
        cell.statusViewModel = listViewModel.statusViewModel[indexPath.row]
        return cell
    }

}
